<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/8
 * Time: 16:49
 */
$startTime = $route->bodyParams['startTime'];  //开始时间
$endTime = $route->bodyParams['endTime'];      //结束时间
$oesid = $route->bodyParams['oesid'];          //机构id
$skip =$route->bodyParams['skip'];
$limit =$route->bodyParams['limit'];


//获取这个机构的所有教室
$sql1 = "SELECT osc_id,osc_name,oc_name,ou_name,osc_official_student_count,oc_limit_number
        FROM v_art_class_info
        WHERE osc_oesid = ".$oesid." AND osc_create_time >= ".$startTime ." AND osc_create_time < ".$endTime." AND is_delete = 0 LIMIT ".$skip.",".$limit ;

//获取到班级的基本信息
$classList=$db->mysqlDB->query($sql1);

//根据班级id 把课时id拿到 还有每个班的所有学生id
foreach( $classList as $key1 => $value1 ){
    $stopStuCount = 0;
    $sql2 = "SELECT oct_id FROM art_oa_class_time WHERE oct_oscid = ".$value1["osc_id"]." AND oct_start_time >= ".$startTime." AND oct_start_time <".$endTime;
    $classTimeData[$key1]=$db->mysqlDB->query($sql2);
    $sql3 = "SELECT ocs_osid FROM art_oa_class_student WHERE ocs_oscid = ".$value1["osc_id"];
    $classStudentData[$key1]=$db->mysqlDB->query($sql3);
    //查询每个班级的流失人数
    $sql5 = "SELECT COUNT(*) AS num FROM art_oa_stop_class_record WHERE oscr_classid = ".$value1["osc_id"];
    $num=$db->mysqlDB->query($sql3);
    $stopStuCount += $num[0]['num'];
    $classList[$key1]['stopStuCount'] = $stopStuCount;
}
//查询每个班中的回访次数
for($i = 0 ; $i < count($classStudentData) ; $i++ ){
    $temp = 0;
    $arrNum = 0;
    foreach ($classStudentData[$i] as $key2 => $value2){
        //查询每个学生被回访的次数
        $sql4 = "SELECT COUNT(*) AS num FROM art_oa_return_record WHERE orr_stuid = ".$value2["ocs_osid"];
       
        $return=$db->mysqlDB->query($sql4);
        $temp = $temp + $return[0]['num'];
    }
    $classList[$i]["returnCount"] = $temp;
    foreach ($classTimeData[$i] as $key3 => $value3){
        //查询每个学生被回访的次数
        $sql5 = "SELECT COUNT(*) AS num FROM art_oa_student_class_time_check WHERE osctc_octid = ".$value3["oct_id"]." AND osctc_status = 0 AND is_delete = 0";
        $arrive=$db->mysqlDB->query($sql5);
        $arrNum = $arrNum + $arrive[0]['num'];
    }
    $classList[$i]["attendance"] = round($arrNum / ($classList[$i]["osc_official_student_count"] * count($classTimeData[$i])),2);
}


$response->responseData(true, $classList);